package oop;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class ProductFind extends JFrame{
    
    private static final long serialVersionUID = 1L;
    private Font f;
    private JPanel p;
    private JLabel Label_Header,Label_Search,Label_Type,Label_Brand,Label_Model;
    private JComboBox<String> Combo_Type,Combo_Brand;
    private JList<String> List_Model;
    private JScrollPane scoreOflist;
    private JButton Button_ok,Button_cancel;
    String NullString[] = {""};
    String Type[] = {"กรุณาเลือกชนิดสินค้า","เครื่องสาย","เครื่องเป่า","เครื่องลิ่มนิ้ว","เครื่องกระทบ"};
    String Brand[];
    String Model[];
    
    public ProductFind(){
        super("Product Find");
        setLayout(null);
        p = new JPanel();
        p.setLayout(null);
        p.setBorder(BorderFactory.createTitledBorder(null, "Products Find", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,new Font("Segoe UI",Font.PLAIN,14)));
        p.setBounds(5, 5, 389, 233);
        setComponent();
        add(p);
        setBounds(455,160,415,280);
    }
    
    public void setComponent(){
        
        f = new Font("Tahoma",Font.PLAIN,12);
        
        //create
        Label_Header = new JLabel("PRODUCTS FIND");             
        Label_Search = new JLabel("ค้นหาสินค้า..");             
        Label_Type = new JLabel("ประเภทสินค้า:");               
        Label_Brand = new JLabel("ยี่ห้อ:");            
        Label_Model = new JLabel("รุ่น:");   
        
        //set data in component
      //set data in component
        Combo_Type = new JComboBox<String>(NullString);  
        Combo_Type.setMaximumRowCount(3);
        Combo_Type.removeItem(NullString[0]);
        for(int i = 0;i<Type.length;i++)
        	Combo_Type.addItem(Type[i]);
        

        Combo_Brand = new JComboBox<String>(NullString);          
        Button_ok = new JButton("ตกลง");                
        Button_cancel = new JButton("ยกเลิก");  
        List_Model = new JList<String>();
        List_Model.setVisibleRowCount(3);
        List_Model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //scorbar of list
        scoreOflist = new JScrollPane(List_Model);
        
        //setFont
        Label_Header.setFont(new Font("Segoe UI",Font.BOLD,18));
        Label_Search.setFont(new Font("Tahoma",Font.PLAIN,11));
        Label_Type.setFont(f);
        Label_Brand.setFont(f); 
        Label_Model.setFont(f);
        Combo_Type.setFont(f);
        Combo_Brand.setFont(f);
        Button_ok.setFont(new Font("Tahoma",Font.PLAIN,11));
        Button_cancel.setFont(new Font("Tahoma",Font.PLAIN,11));
        List_Model.setFont(f);
        
        //setBounds
        Label_Header.setBounds(20, 25, 150, 20);
        Label_Search.setBounds(20, 50, 90, 20);
        Label_Type.setBounds(45, 80, 90, 20);
        Label_Brand.setBounds(90, 115, 90, 20);
        Label_Model.setBounds(100, 155, 90, 20);
        Combo_Type.setBounds(120, 85, 170, 20);
        Combo_Brand.setBounds(120, 120, 170, 20);
        scoreOflist.setBounds(120, 160, 170, 70);
        Button_ok.setBounds(300, 85, 85,25);
        Button_cancel.setBounds(300, 115, 85,25);
        
        //setIcon
        Button_ok.setIcon(new javax.swing.ImageIcon("D:\\Project\\src\\pic\\ok.png"));
        Button_cancel.setIcon(new javax.swing.ImageIcon("D:\\Project\\src\\pic\\Delete.png"));
        
        //add
        add(Label_Header);
        add(Label_Search);
        add(Label_Type);
        add(Label_Brand);
        add(Label_Model);
        add(Combo_Type);
        add(Combo_Brand);
        add(scoreOflist);
        add(Button_ok);
        add(Button_cancel);
        
        Combo_Type.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				Combo_Brand.removeAllItems(); //clear list in combo_bland
				List_Model.clearSelection();
				List_Model.setListData(NullString);
				if(e.getItem() == Combo_Type.getItemAt(1)){
					//JOptionPane.showMessageDialog(null, Setup.CountDATA("select COUNT(DISTINCT Product_code)AS CountCODE from MSLM_MAIN_PRODUCT"));
					
					// Brand is array.Therefor,it is call function CountDATA in "CLASS SETUP" for count size
					Brand = new String[Setup.CountDATA("select COUNT(DISTINCT PRODUCT_CODE)AS CountCODE from MSLM_MAIN_PRODUCT","CountCODE")];
			        
		    		try{
		    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		    			Connection con = DriverManager.getConnection("jdbc:odbc:MSL");
		    			Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
		    			ResultSet re;
		    			for(int i = 0;i < Brand.length;i++){
			    			re = stmt.executeQuery("select PRODUCT_NAME from MSLM_MAIN_PRODUCT where '"+(i+1)+"' = PRODUCT_ID");
			    			re.first();
			    			
			    			Brand[i] = re.getString("PRODUCT_NAME");
			    			
			    			re.close();
		    			}
						for(int i = 0;i<Brand.length;i++)
							Combo_Brand.addItem(Brand[i]);
		    			con.close();
		    		}
		    		catch(SQLException s){
		    			JOptionPane.showMessageDialog(null, "SQL_ERROR", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
		    		}
		    		catch(ClassNotFoundException c){
		    			JOptionPane.showMessageDialog(null, "CLASS_NOTFOUND", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
		    			
		    		}
				}//end if
				
				
			}
		});
        
        
        
        Combo_Brand.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				if(e.getItem() == Combo_Brand.getSelectedItem()){
					
					Model = new String[Setup.CountDATA("select COUNT(DISTINCT STOCK_ID)AS CountCODE from MSLT_MAIN_PRODUCT_STOCK_"+e.getItem()+"","CountCODE")];
					
					try{
		    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		    			Connection con = DriverManager.getConnection("jdbc:odbc:MSL");
		    			Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
		    			ResultSet re;
		    			for(int i = 0;i < Model.length;i++){
			    			re = stmt.executeQuery("select STOCK_NAME_PRODUCT from MSLT_MAIN_PRODUCT_STOCK_"+e.getItem()+" where '"+(i+1)+"' = STOCK_ID");
			    			re.first();
			    			
			    			Model[i] = re.getString("STOCK_NAME_PRODUCT");
			    			
			    			re.close();
		    			}
		    			con.close();
		    			List_Model.setListData(Model);
		    		}
		    		catch(SQLException s){
		    			JOptionPane.showMessageDialog(null, "SQL_ERROR", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
		    		}
		    		catch(ClassNotFoundException c){
		    			JOptionPane.showMessageDialog(null, "CLASS_NOTFOUND", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
		    		}
		    	}
			}
		});
        }
    }
